package com.weihua.flow;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class FlowCountMapper extends Mapper<LongWritable, Text, Text, FlowBean> {
    Text mapKey = new Text();
    FlowBean mapValue = new FlowBean();

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        //1、读取一行数据并转换string类型
        String line = value.toString();

        //2、分词
        String[] words = line.split("\t");

        //3、提取数据并将数据封装到对象;
        mapKey.set(words[1]);
        //倒数第三个长度索引，上行流量
        Long upFlow = Long.parseLong(words[words.length - 3]);
        long downFlow = Long.parseLong(words[words.length - 2]);

        mapValue.set(upFlow, downFlow);

        //4、将中间结果手机号、FlowBean写入到context
        context.write(mapKey, mapValue);
    }
}

